import { reqGetAddressList,reqGetOrder,reqCommitOrder } from "@/api"

const trade= {
    state: {
        addressList:[],
        order:{}
    },
    mutations: {
        GETADDRESSLIST(state,addressList){
            state.addressList=addressList
        },
        GETORDER(state,order){
            state.order=order
        }
    },
    actions: {
        async getAddressList({commit}){
            let res= await reqGetAddressList()
            if(res.message==='成功'){
                commit('GETADDRESSLIST',res.data)
                return 'ok'
            }else{
                throw new Error('获取地址失败')
            }
            
        },
        async getOrder({commit}){
            let res=await reqGetOrder()
            if(res.code===200){
                commit('GETORDER',res.data)
                return 'ok'
            }else{
                throw new Error('fail')
            }
        },
        async commitOrder({commit},data){
            let res= await reqCommitOrder(data)
            if(res.code===200){
                return res.data
            }else{
                throw new Error('提交订单失败')
            }
        }
    },
    getters: {
        detailArrayList(state){
            return state.order.detailArrayList
        },
        totalAmount(state){
            return state.order.totalAmount
        },
        totalNum(state){
            return state.order.totalNum
        },
        tradeNo(state){
            return state.order.tradeNo
        }
    }
}
export default trade